-- Chesterfield the Immortal Mod
-------------------------------------------------------------------------------
-- Description:
--
-- Mod to make chester immortal, i.e. unable to die as well as ignored by other
-- monsters and generally just being his faithful self.
--
-------------------------------------------------------------------------------
-- Features:
--
-- * Chester is no longer targetable, burnable, engaged in combat as a target
--   or killable (actually just has really high hitpoints for now due to game bug)
--
-- * IN FUTURE:
--   Be visible on the map.  For now, place a trap in him so he shows up on the map
--
-------------------------------------------------------------------------------
-- Installation:
--
-- * Exit the game if you have not already done so.
--
-- *** MAKE SURE TO BACKUP YOUR SAVED GAMES AND GAME PROFILE WHEN INSTALLING ANY MOD ***
--     they are either in your steam folder under "\userdata\<<your steam id>>\219740\remote" or in your documents folder under "\Klei\DoNotStarve\save"
--
-- * Locate the Don't Starve installation folder, usually here "C:\Program Files (x86)\steam\steamapps\common\dont_starve\"
--     (if not sure, go to Steam client, right-click on game under Library section, select Properties, select "LOCAL FILES" tab, select "BROWSE LOCAL FILES..." button)
--
-- * Extract zip file to the Don't Starve mods folder (or manually extract the files if you prefer)
--
-- * Open the modsettings.lua files in the "...\data\scripts\mods" folder
--   - Add the following statement on a seperate line NOT including the "--" and save the file:
--        ModManager:AddMod("Chesterfield the Immortal")
--
-- * Run the game and enjoy!
--
-------------------------------------------------------------------------------
-- Compatibility:
--
-- Don't Starve Revision 73662-73920 2013-03-01_20-16-52 (Winter Update)
--
-- -no known incompatibilites-
--
-------------------------------------------------------------------------------
-- Changelog:
--
-- v1.1
-- Fixed so attackers properly ignore chester
--
-- v1.0
-- Initial release for Winter Update
--
-------------------------------------------------------------------------------
-- Support:
--
-- Report issues found or suggestions to this thread on Klei's forums
-- http://forums.kleientertainment.com/showthread.php?11697
--
-------------------------------------------------------------------------------
-- Credits:
-- clockwork for his original Better Chester mod that is mod is based on
--
-------------------------------------------------------------------------------
--
-- By WrathOf
-- Version 1.1
-- Updated: 2013-03-06
--
-------------------------------------------------------------------------------


-- custom prefab post init function to handle our change
local function trapteethPrefabPostInit(inst)

	--Replace the game's OnExplode callback function with one the verifies that
	--target has a combat component
	if inst.components.mine then
		inst.components.mine:SetOnExplodeFn(function (inst, target)
--print("using custom on explode fn")
			inst.AnimState:PlayAnimation("trap")
			if target and target.components.combat then
				inst.SoundEmitter:PlaySound("dontstarve/common/trap_teeth_trigger")
				target.components.combat:GetAttacked(inst, TUNING.TRAP_TEETH_DAMAGE)
			end
			if inst.components.finiteuses then
				inst.components.finiteuses:Use(1)
			end
		end)
	end

end

--add a post init function for the tooth trap prefab to call our function when it is done getting setup
--AddPrefabPostInit("trap_teeth", trapteethPrefabPostInit)
--AddPrefabPostInit("trap_teeth_maxwell", trapteethPrefabPostInit)


-- custom prefab post init function to handle our change
function chesterPrefabPostInit(inst)

	if inst then

		--Remove character tag so attackers ignore chester since health settings not working
		inst:RemoveTag("character")

		inst:RemoveComponent("burnable")

		--comment out these two lines if want the combat feature intact (but this has not been tested)
		if not inst:HasTag("notarget") then inst:AddTag("notarget") end
		inst:RemoveComponent("combat")

		--TODO: determine why not working
		TUNING.CHESTER_HEALTH = 1000

		--WORKAROUND: Set health parameters very high until game is patched to allow removal of health component
		if inst.components.heath then
			--inst.components.health:StopRegen()
			inst.components.health.invincible = true
    		--TODO: determine why not working
    		inst.components.health:SetMaxHealth(1000)
			--inst.components.health.maxhealth = 0
    		--inst.components.health.currenthealth = 0
			--Make invulnerable to heat and fire
	    	inst.components.health.vulnerabletoheatdamage = false
			inst.components.health.fire_damage_scale = 0
			inst.components.health.penalty = 0
		end

	end

end

--add a post init function for the chester prefab to call our function when it is done getting setup
AddPrefabPostInit("chester", chesterPrefabPostInit)

